require.config({
    baseUrl: '../javascripts/library', // 基础路径
    paths: {
        jquery: 'jquery',
        axios: 'axios',
    },

});

require(['jquery', 'axios'], function($, axios) {
    $('.getcode').click(() => {

        let username = $('.email').val();
        if (username == '') {
            // $('.e-tx').text('请输入邮箱');
        } else {
            axios.get('/users/getcode', { params: { username: username } }).then(res => {
                console.log(res.data.success)
                if (res.data.success == 0) {
                    alert('该账号已存在，请登录');
                    $('.email').val('');
                    location.href = 'http://localhost:3000/html/登录页面.html'
                };
            }).catch(err => {
                console.log(err)
            })
        }

    })


});


require(['jquery', 'axios'], function($, axios) {
    $('.sub').click(() => {
        let username = $('.email').val();
        let psw = $('.psw').val();
        let code = $('.code').val();
        axios.post('/users/register', { params: { username: username, password: psw, code: code } }).then(res => {
            console.log(res.data.success);
            let success = res.data.success;
            switch (success) {
                case 0:
                    $('.email').val('');
                    alert('邮箱错误请重新输入');
                    break;
                case 1:
                    $('.email').val('');
                    $('.psw').val('');
                    $('.code').val('');
                    alert('注册成功请登录')
                    location.href = 'http://localhost:3000/html/登录页面.html'
                    break;
                case 2:
                    $('.code').val('');
                    alert('验证码错误请重新输入');
                    break;
            }
        }).catch(err => {
            console.log(err);
        })
    })
})


require(['jquery', 'axios'], function($, axios) {
    $('.denglu').on('click', () => {

        let username = $('.username').val();
        let psw = $('.psw').val();
        console.log(psw)
        axios.get('/users/login', { params: { username: username, password: psw } }).then(res => {
            let num = res.data.success;
            let token = res.data.token;
            console.log(res.data.token)
            localStorage.setItem('username', token);
            num ? location.href = 'http://localhost:3000/' : alert('账号或密码错误');
            $('.name').val('');
            $('.psw').val('');
        }).catch(err => {
            console.log(err)

        })
    })


})